module Display {
	export class MaskTest extends egret.Sprite{
		private _txInfo:egret.TextField;
		private _hero:egret.Bitmap;
		private _shp:egret.Shape;

		/**
		 * 构造函数
		 * 监听添加到舞台事件
		 */
		public constructor() {
			super();
			this.addEventListener(egret.Event.ADDED_TO_STAGE,this.onAddedToStage,this);
		}

		/**
		 * 添加到舞台监听器
		 * 加载图片 
		 * @param evt:Event
		 */
		private onAddedToStage(evt:egret.Event):void{
			var imgLoader:egret.ImageLoader=new egret.ImageLoader;
			imgLoader.once(egret.Event.COMPLETE,this.imgLoaderHandler,this);
			imgLoader.load("resource/hero.png");
		}

		/**
		 * 图片加载监听器
		 * @param evt:Event
		 */
		private imgLoaderHandler(evt:egret.Event):void{
			//背景
			var bg:egret.Shape=new egret.Shape;
			bg.graphics.beginFill(0xaabbcc);
			bg.graphics.drawRect(0,0,this.stage.stageWidth,this.stage.stageHeight);
			bg.graphics.endFill();
			this.addChild(bg);
			//显示图片
			this._hero=new egret.Bitmap(evt.currentTarget.data);
			this.addChild(this._hero);
			this._hero.anchorOffsetX=this._hero.width/2;
			this._hero.anchorOffsetY=this._hero.height/2;
			this._hero.x=this.stage.stageWidth/2;
			this._hero.y=this.stage.stageHeight*0.6;
			//提示信息
			this._txInfo=new egret.TextField;
			this._txInfo.size=18;
			this._txInfo.x=50;
			this._txInfo.y=150;
			this._txInfo.textAlign=egret.HorizontalAlign.LEFT;
			this._txInfo.textColor=0xff0000;
			this._txInfo.lineSpacing=6;
			this._txInfo.multiline=true;
			this._txInfo.width=this.stage.stageWidth-100;			
			this._txInfo.text="接触屏幕后白鹭小鸟将变为椭圆形状的遮罩区域，可以移动手指（白鹭小鸟）并观察椭圆在遮罩下的显示变化";
			this.addChild(this._txInfo);
			//椭圆
			this._shp=new egret.Shape();
			this._shp.graphics.clear();
			this._shp.graphics.lineStyle(3,0x000000);
			this._shp.graphics.beginFill(this.getRandomColor());
			this._shp.graphics.drawEllipse(-100,-150,200,300);
			this._shp.x=this._hero.x;
			this._shp.y=this._hero.y;
			this.addChildAt(this._shp,1);
			//添加触摸事件
			this.stage.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.touchHandler,this);
		}

		/**
		 * 得到随机颜色
		 */
		private getRandomColor():number{
			return (Math.floor(Math.random()*0xff)<<16)+
				(Math.floor(Math.random()*0xff)<<8)+
				Math.floor(Math.random()*0xff);
		}

		/**
		 * 触摸事件
		 */
		private touchHandler(evt:egret.TouchEvent):void{
			switch(evt.type){
				case egret.TouchEvent.TOUCH_BEGIN:
					this.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE,this.touchHandler,this);
					this.stage.once(egret.TouchEvent.TOUCH_END,this.touchHandler,this);
					this._hero.x=evt.stageX;
					this._hero.y=evt.stageY;
					this._shp.mask=this._hero;
					break;
				case egret.TouchEvent.TOUCH_MOVE:
					this._hero.x=evt.stageX;
					this._hero.y=evt.stageY;
					break;
				case egret.TouchEvent.TOUCH_END:
					this.stage.removeEventListener(egret.TouchEvent.TOUCH_MOVE,this.touchHandler,this);
					this._shp.mask=null;
					this._hero.$maskedObject=null;
					break;
				default:
					break;
			}
		}
	}
}